package com.hzmall.util;

import com.hzmall.common.RedisShardedPool;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import redis.clients.jedis.ShardedJedis;
import sun.rmi.runtime.Log;

@Slf4j
public class RedisShardedPoolUtil {

    //Redis set命令
    public static String set(String key, String value) {
        ShardedJedis jedis = null;
        String result = StringUtils.EMPTY;
        try {
            jedis = RedisShardedPool.getJedis();
            result = jedis.set(key, value);
        } catch (Exception e) {
            log.error("Redis执行set命令异常", e);
            RedisShardedPool.returnBrokenResource(jedis);
        }
        RedisShardedPool.returnResource(jedis);
        return result;
    }

    //Redis get命令
    public static String get(String key) {
        ShardedJedis jedis = null;
        String result = StringUtils.EMPTY;
        if (StringUtils.isNotBlank(key)) {
            try {
                jedis = RedisShardedPool.getJedis();
                result = jedis.get(key);
            } catch (Exception e) {
                log.error("Redis执行get命令异常", e);
                RedisShardedPool.returnBrokenResource(jedis);
            }
            RedisShardedPool.returnResource(jedis);
        }
        return result;
    }

    //Redis setNx命令,若键值对存在，设置失败，返回0
    public static Long setNx(String key, String value) {
        ShardedJedis jedis = null;
        Long result = null;
        try {
            jedis = RedisShardedPool.getJedis();
            result = jedis.setnx(key, value);
        } catch (Exception e) {
            log.error("Redis执行setNx命令异常", e);
            RedisShardedPool.returnBrokenResource(jedis);
        }
        RedisShardedPool.returnResource(jedis);
        return result;
    }

    //Redis setEx命令,设置一个有效期限为exTime的键值对
    public static String setEx(String key, String value, Integer exTime) {
        ShardedJedis jedis = null;
        String result = StringUtils.EMPTY;
        try {
            jedis = RedisShardedPool.getJedis();
            result = jedis.setex(key, exTime, value);
        } catch (Exception e) {
            log.error("Redis执行setEx命令异常", e);
            RedisShardedPool.returnBrokenResource(jedis);
        }
        RedisShardedPool.returnResource(jedis);
        return result;
    }

    //Redis expire命令 对给定的key，设置生存时间，到时间后自动删除
    public static Long expire(String key, Integer exTime) {
        ShardedJedis jedis = null;
        Long result = null;
        try {
            jedis = RedisShardedPool.getJedis();
            result = jedis.expire(key, exTime);
        } catch (Exception e) {
            log.error("Redis执行Expire命令异常", e);
            RedisShardedPool.returnBrokenResource(jedis);
        }
        RedisShardedPool.returnResource(jedis);
        return result;
    }

    //Redis执行del命令
    public static Long del(String key) {
        ShardedJedis jedis = null;
        Long result = null;
        try {
            jedis = RedisShardedPool.getJedis();
            result = jedis.del(key);
        } catch (Exception e) {
            log.error("Redis执行Expire命令异常", e);
            RedisShardedPool.returnBrokenResource(jedis);
        }
        RedisShardedPool.returnResource(jedis);
        return result;
    }

    //Redis执行getSet命令，设置新值的同时返回旧值
    public static String getSet(String key, String value) {
        ShardedJedis jedis = null;
        String result = StringUtils.EMPTY;
        try {
            jedis = RedisShardedPool.getJedis();
            result = jedis.getSet(key, value);
        } catch (Exception e) {
            log.error("Redis执行Expire命令异常", e);
            RedisShardedPool.returnBrokenResource(jedis);
        }
        RedisShardedPool.returnResource(jedis);
        return result;
    }
}
